import { createApp } from 'vue'
import App from './App.vue'
import '@/assets/less/index.less'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
//默认找index.vue作为模板文件
import router from './router'
import { createPinia } from 'pinia';

import api from "@/api/api.js"
import { useAllDataStore } from "@/stores/index.js";

function isRoute(to){
  // return router.getRoutes().filter(item=>item.path === to.path).length > 0
  let res = router.getRoutes();
  let resFil = res.filter(item => item.path === to.path);
  return resFil.length > 0;
}

router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && !store.state.token) {
    console.log(store.state.token)
    return { name: 'login' }
  }
  if (!isRoute(to)) {
    return { name: '404' }
  }
  next();
})


const app = createApp(App)

//注册，全局使用，约定全局$
app.config.globalProperties.$api = api;

app.use(ElementPlus)

const pinia = createPinia();
app.use(pinia);

//实现持久化存储路由
const store = useAllDataStore();
store.addMenu(router,"refresh")

// 遍历ElementPlusIconsVue对象的所有键值对
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    // 在应用中注册组件，使用其键作为名称
    app.component(key, component);
}
app.use(router);
app.mount('#app');

